import tkinter as tk
import tkinter.font as tkFont
import tkinter.messagebox as tkMsgBox
import json
import os
import random
#####
with open(file = './ques.json', mode = 'r', encoding = 'utf8') as f: #读入文档
    ftext = f.read()

data = json.loads(ftext)
a=data['ques'][:]
random.shuffle(a)
b=data['ans'][:]
for i in range(0,len(data['ans'])):
    data['ans'][i]=b[data['ques'].index(a[i])]
data['ques']=a[0:len(data['ques'])//3*2]
data['ans']=data['ans'][0:len(data['ans'])//3*2]
#####
top = tk.Tk() #tk 跑龙套
top.title( data['name'] )
top.resizable(False, False)
gf = tkFont.Font(top, size = 15)
#####
text = data['text'] #显示文字
frame = []
for i in range(len(text)):
    frame1 = tk.Frame(top)
    frame1.pack(side = tk.TOP, fill = tk.X, padx = 5)
    frame.append(frame1)
    
for i in range(len(text)):
    label = tk.Label(frame[i], text = text[i], anchor = tk.W, font = gf)
    label.pack(side = tk.TOP, fill = tk.X)
#####
ques = data["ques"] #取出问题
ans = []

for i in range(len(ques)): #初始化答案
    ans1 = tk.IntVar()
    ans1.set(0)
    ans.append(tk.IntVar())
#####
question = ques[0][0] #问题
option_str = ques[0][1]
question_frame = tk.Frame(top)
question_frame.pack(side = tk.TOP, fill = tk.X)
que = tk.Label(question_frame, text = question, anchor = tk.W, font = gf)
que.pack(side = tk.TOP, fill = tk.X)
option_frame = tk.Frame(question_frame)
option_frame.pack(side = tk.TOP, fill = tk.X)

options = option_str.split(':')
opt = []

for i in range(0,4):
    option = tk.Radiobutton(option_frame, text = options[i], variable = ans[0], value = i+1, font = gf)
    option.pack(side = tk.LEFT)
    opt.append(option)
#####
def ChangeQuestion(top, question, option_str, option_var): #改变问题
    global que, opt
    que.configure(text = question) #问题的文字
    options = option_str.split(':')
    for i in range(0,4): #选项的文字
        opt[i].configure(text = options[i], variable = option_var)
#####
x = 0 #第几页
csans = False #是否已看答案 already see ans
#####
def Last(): #上一页功能实现
    global x, ans, csans
    
    if x == 0: #第一页时不能往上翻页
        return
        
    if x == len(ques)-1: #当在最后一页时翻页,把btn2从"提交"恢复为"下一页"
        btn2.configure(text = "下一页")
    
    if csans == True: #当提交后时,将功能变为[重新再来]
        x = 1 #第82行会-1(  =  0)
        ans = []
        for i in range(len(ques)): #将已有答案清空
            ans1 = tk.IntVar()
            ans1.set(0)
            ans.append(tk.IntVar())
        csans=False
        btn1.configure(text = "上一页") #将btn1与btn2恢复
        btn2.configure(text = "下一页")
    
    x -= 1 #上一页
    ChangeQuestion(top, ques[x][0], ques[x][1], ans[x]) #变换文字
######
def Next(): #下一页功能实现
    global x, btn2, btn1, csans
    
    if x == len(ques)-2: #当在倒数第二页时将文字变为"提交"
        btn2.configure(text = "提交")
    
    if x == len(ques)-1: #当在倒数第一页时,将功能变为[提交]
              
        mark = 0 #分数
        for i in range(len(ans)): #记分
            if ans[i].get() == 0: #如未作答
                tkMsgBox.showerror('error', "你还未作答第" + str(i+1) + "题")
                return
            elif ans[i].get() == data["ans"][i]: #如果答案是对的
                mark += 1
        
        tkMsgBox.showinfo('score', "你答对了" + str(mark) + "题") #显示答对了几题
        
        if mark <= len(ans)//3: #如果"过于优秀"
            tkMsgBox.showinfo('"prive"', "由于你答题过于优异，所以我给你准备了一份礼物呢~")
        
        btn2.configure(text = "查看答案") #提交后改变btn2和btn1的文字
        btn1.configure(text = "重新再来")
        csans = True #已经可以看到答案
        return
        
    x += 1 #下一页
    ChangeQuestion(top, ques[x][0], ques[x][1], ans[x]) #改变文字
#####
btnFrame = tk.Frame(top) #上一页与下一页按钮
btnFrame.pack(side = tk.TOP, fill = tk.X, padx = 6, pady = 6)
btn1 = tk.Button(btnFrame, text = '上一页', command = Last, font = gf)
btn1.pack(side = tk.LEFT)
btn2 = tk.Button(btnFrame, text = '下一页', command = Next, font = gf)
btn2.pack(side = tk.RIGHT)
#####
top.mainloop() #跑龙套